<template>
  <div>
    <lan-header :config="headerConfig"></lan-header>
    <lan-search-form
      size="small"
      :config="searchForm"
      :formData="searchFormData"
      @search="search"
    ></lan-search-form>
    <lan-dialog
      :config="addConfig"
      :formData="addFormData"
    >
      <template #slot="scope">
        <el-input
          v-model="scope.data.slot"
          class="form-input"
        ></el-input>
      </template>
    </lan-dialog>
  </div>
</template>

<script>
import lanSearchForm from '../../../components/lanSearchForm';
import lanHeader from '../../../components/lanHeader';
import { createAreaApi } from '../../../api/area';
import moment from 'moment';
import { MomentFormat, SortType } from '../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { parseError } from '../../../utils';
import { queryAreaListApi } from '../../../api/area';
export default {
  components: {
    lanHeader,
    lanSearchForm
  },
  props: {
    searchFormData: {
      type: Object,
      default() {
        return {};
      }
    }
  },

  data() {
    return {
      headerConfig: {
        title: '工厂列表',
        buttons: [
          {
            text: '新建',
            icon: 'add',
            type: 'primary',
            size: 'small',
            method: () => {
              this.showAdd();
            }
          }
        ]
      },

      searchForm: {
        inline: true,
        size: 'small',
        formList: [
          {
            name: 'areaUuid',
            type: 'remoteSelect',
            options: () => this.areaList,
            optionValue: 'uuid',
            optionLabel: 'name',
            placeholder: '区域',
            query: () => {
              this.remoteAreaSearch();
            }
          },
          {
            name: 'name',
            type: 'input',
            placeholder: '工厂名称'
          }
        ]
      },

      areaList: []
    };
  },
  methods: {
    search() {
      this.$emit('search');
    },
    remoteAreaSearch(query) {
      this.searchFormData.remoteLoading = true;
      queryAreaListApi(query, 10)
        .then(areaList => (this.areaList = areaList))
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.remoteLoading = false));
    },
    showAdd() {
      this.$router.push('/factory/create');
    }
  }
};
</script>

<style>
</style>
